package com.zhixue.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther: 李 力
 * @Date: 2025/4/8
 * @Description: com.zhixue.exception
 * @version: 1.0
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    //自定义异常
    @ResponseBody
    @ExceptionHandler(ZhixuePlusException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public RestErrorResponse customException(ZhixuePlusException exception) {
        RestErrorResponse response = new RestErrorResponse(exception.getErrMessage());
        return response;
    }

    //其他异常
    @ResponseBody
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public RestErrorResponse exception(Exception exception) {
        RestErrorResponse response = new RestErrorResponse(CommonError.UNKOWN_ERROR.getErrMessage());
        return response;
    }
}
